#include "mgxEdit.h"

// Aplicacion principal
GGE mgxEditorMainApp;
const char *  WINDOW_CAPTION  = "mgxEditor";
int  WINDOW_FLAG       = GGE_VIDEO | GGE_SND | GGE_TTF;
int  WINDOW_MODE      = GGE_MODE2D_WIN;
int  WINDOW_RES_W    = 1280;
int  WINDOW_RES_H     = 720;
int  WINDOW_BPP         = 32;
bool  WINDOW_LOOP     = 0;
float fOn = 0.0f;
float fOff = 1.0f;
bool exitf = 0;
bool editorexit=0;
// Interfaz de entrada/salida
GGE_eMotion     mgxEditorMainDevice;

// Mensajes en pantalla
GGE_Text          mgxEditorMainMSG;

GGE_TEXTURE mgxEditorMMTextures[255];
GGE_Entity2D  mgxEditorMMContent[255];

void mgxInit()
{
  // Inicia la Aplicacion
  // Aqui se inician todos los sistemas para que el editor funcione.
  mgxEditorMainApp.Init ( WINDOW_FLAG );
  mgxEditorMainApp.Mode ( WINDOW_MODE );
  mgxEditorMainApp.Screen ( WINDOW_RES_W, WINDOW_RES_H, WINDOW_BPP, WINDOW_CAPTION );
}

void mgxLoad ()
{
  // Cargamos todos los ficheros necesarios para la ejecucion del programa
  // Carga la fuente TTF
  mgxEditorMainMSG.Load ( "Ubuntu-L.ttf", 12 );
  // Carga los elementos graficos necesarios para el editor
  mgxEditorMMTextures[0].Load ( "data/img/door-lft.png");
  mgxEditorMMTextures[1].Load ( "data/img/door-rght.png");
  mgxEditorMMTextures[2].Load ( "data/img/logo001.png");
  mgxEditorMMTextures[3].Load ( "data/img/logo002.png");
  mgxEditorMMTextures[4].Load ( "data/img/shine.png");
  mgxEditorMMTextures[5].Load ( "data/img/tb.png");
  mgxEditorMMTextures[6].Load ( "data/img/temas[des].png");
  mgxEditorMMTextures[7].Load ( "data/img/temas[sen].png");

  mgxEditorMMContent[0].Addframe( mgxEditorMMTextures[0] );
  mgxEditorMMContent[1].Addframe( mgxEditorMMTextures[1] );
  mgxEditorMMContent[2].Addframe( mgxEditorMMTextures[2] );
  mgxEditorMMContent[3].Addframe( mgxEditorMMTextures[3] );
  mgxEditorMMContent[4].Addframe( mgxEditorMMTextures[4] );
  mgxEditorMMContent[5].Addframe( mgxEditorMMTextures[5] );
  mgxEditorMMContent[6].Addframe( mgxEditorMMTextures[6] );
  mgxEditorMMContent[7].Addframe( mgxEditorMMTextures[7] );
}

void mgxEditor()
{

  //mgxMenu();

  mgxEditorON();
  mgxEditorMainApp.Exit();
}

void mgxMenu()
{
  // Fade 0n de la imagen de logo001
  fOn = 0.0f;
  fOff = 1.0f;
  while (  fOn <= 1.0f )
    {
      mgxEditorMainApp.Clear();
      mgxEditorMMContent[2].Alpha ( fOn );
      mgxEditorMMContent[2].Draw ( 512,384 );
      mgxEditorMainApp.Flip();
      fOn+=.01;
    }

  // Se espera a q se pulse una tecla
  mgxEditorMainApp.Loop();

  // Fade Off
  while (  fOff >=0.0f )
    {

      mgxEditorMainApp.Clear();
      mgxEditorMMContent[2].Alpha ( fOff );
      mgxEditorMMContent[2].Draw ( 512,384 );
      mgxEditorMainApp.Flip();
      fOff-=.01;
    }

  // Fade On de la imagenes de la intro
  fOn = 0.0f;
  while (  fOn <= 1.0f )
    {
      mgxEditorMainApp.Clear();
      mgxEditorMMContent[0].Alpha ( fOn );
      mgxEditorMMContent[0].Draw ( 262,384 );
      mgxEditorMMContent[1].Alpha ( fOn );
      mgxEditorMMContent[1].Draw ( 802,384 );
      mgxEditorMainApp.Flip();
      fOn+=.01;
    }
  float x=262;
  float xx = 802;
  while ( x>=-320 )
    {
      mgxEditorMainApp.Clear();
      mgxEditorMMContent[3].Draw ( 512  ,384 );
      mgxEditorMMContent[0].Draw ( x ,384 );
      mgxEditorMMContent[1].Draw ( xx  ,384 );

      mgxEditorMainApp.Flip();
      x-=4;
      xx+=4;
    }

  // Entra en el bucle del menu de juego.
  while ( !exitf )
    {
      mgxEditorMainApp.Clear();
      mgxEditorMMContent[3].Draw ( 512  ,384 );
      if ( mgxEditorMainDevice.MouseX() >= 473 &&
           mgxEditorMainDevice.MouseX() <= 631 &&
           mgxEditorMainDevice.MouseY() >= 655 &&
           mgxEditorMainDevice.MouseY() <= 670 )
        {
          if ( mgxEditorMainDevice.KeyPress()==gkey_MOUSE_BTT_LFT )
            {
              editorexit = 0;
              mgxEditorON();
            }
          mgxEditorMMContent[4].Draw ( 545  ,663 );
        }

      if ( mgxEditorMainDevice.MouseX() >= 473 &&
           mgxEditorMainDevice.MouseX() <= 631 &&
           mgxEditorMainDevice.MouseY() >= 687 &&
           mgxEditorMainDevice.MouseY() <= 703 )
        {
          if ( mgxEditorMainDevice.KeyPress()==gkey_MOUSE_BTT_LFT )
            {
              exitf = 1;
            }
          mgxEditorMMContent[4].Draw ( 545  ,693 );
        }

      if ( mgxEditorMainDevice.KeyPress()==gkey_ESC )
        {
          exitf = 1;
        }
      mgxEditorMainApp.Flip();
    }
}

void mgxEditorON()
{

  int Temas_des = 0;
// Editor principal
  while ( !editorexit )
    {


      // Limpiamos la pantalla
      mgxEditorMainApp.Clear();

      // dibuja el fondo
      mgxEditorMMContent[5].Draw ( 640 ,  360 );

      // Dibuja las coordenadas
      // mgxEditorMainMSG.Write ( mgxEditorMainDevice.MouseX(), 180, 54) ;
      //mgxEditorMainMSG.Write ( mgxEditorMainDevice.MouseY(), 220, 54) ;

      // Inicio desplegable Tema
      if ( Temas_des == 0 &&
           mgxEditorMainDevice.MouseX() >= 16 &&
           mgxEditorMainDevice.MouseX() <= 47 &&
           mgxEditorMainDevice.MouseY() >= 32 &&
           mgxEditorMainDevice.MouseY() <= 47 )
        {
          Temas_des = 1;
        }

      if ( Temas_des == 1 &&
           mgxEditorMainDevice.MouseY() <= 35 ||
           mgxEditorMainDevice.MouseY() >= 165 ||
           mgxEditorMainDevice.MouseX() <= 7 ||
           mgxEditorMainDevice.MouseX() >= 158)
        {
          Temas_des = 0;
        }

      if(  Temas_des == 1 )
        {
          mgxEditorMMContent[6].Draw ( 84 ,  98 );
          // Opcion Abrir
          if ( Temas_des == 1 &&
               mgxEditorMainDevice.MouseX() >= 12 &&
               mgxEditorMainDevice.MouseX() <= 152 &&
               mgxEditorMainDevice.MouseY() >= 52 &&
               mgxEditorMainDevice.MouseY() <= 61 )
            {
              mgxEditorMMContent[7].Draw ( 84 ,57 );
            }

          // Opcion Cerrar
          if ( Temas_des == 1 &&
               mgxEditorMainDevice.MouseX() >= 12 &&
               mgxEditorMainDevice.MouseX() <= 152 &&
               mgxEditorMainDevice.MouseY() >= 69 &&
               mgxEditorMainDevice.MouseY() <= 81 )
            {
              mgxEditorMMContent[7].Draw ( 84 ,75 );
            }

          // Opcion guardar
          if ( Temas_des == 1 &&
               mgxEditorMainDevice.MouseX() >= 12 &&
               mgxEditorMainDevice.MouseX() <= 152 &&
               mgxEditorMainDevice.MouseY() >= 86 &&
               mgxEditorMainDevice.MouseY() <= 98 )
            {
              mgxEditorMMContent[7].Draw ( 84 ,91 );
            }

          // Opcion eliminar
          if ( Temas_des == 1 &&
               mgxEditorMainDevice.MouseX() >= 12 &&
               mgxEditorMainDevice.MouseX() <= 152 &&
               mgxEditorMainDevice.MouseY() >= 103&&
               mgxEditorMainDevice.MouseY() <= 115 )
            {
              mgxEditorMMContent[7].Draw ( 84 ,107 );
            }
          // Opcion acerca de..
          if ( Temas_des == 1 &&
               mgxEditorMainDevice.MouseX() >= 12 &&
               mgxEditorMainDevice.MouseX() <= 152 &&
               mgxEditorMainDevice.MouseY() >= 133&&
               mgxEditorMainDevice.MouseY() <= 143 )
            {
              mgxEditorMMContent[7].Draw ( 84 ,138 );
            }

          // Opcion salir
          if ( Temas_des == 1 &&
               mgxEditorMainDevice.MouseX() >= 12 &&
               mgxEditorMainDevice.MouseX() <= 152 &&
               mgxEditorMainDevice.MouseY() >= 148&&
               mgxEditorMainDevice.MouseY() <= 160 )
            {
              mgxEditorMMContent[7].Draw ( 84 ,155 );
            }
        }
      // Fin Desplegable Tema

      if ( mgxEditorMainDevice.KeyPress()==gkey_q )
        {
          editorexit = 1;
          exitf = 0;
        }
      mgxEditorMainApp.Flip();
    }

}

void mgxEditorOFF()
{

}

